package io.renren.modules.bs.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import io.renren.modules.bs.entity.BsDepartmentPatientEntity;
import io.renren.modules.bs.vo.BsDepartmentPatientPageVo;
import io.renren.modules.bs.vo.ExtraPatientVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * 
 * 
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2019-08-15 23:03:15
 */
@Mapper
public interface BsDepartmentPatientDao extends BaseMapper<BsDepartmentPatientEntity> {

    @Select("<script>" +
            "SELECT dp.*,dept.hospital_id FROM bs_department_patient dp\n" +
            "LEFT JOIN bs_department dept on dp.department_id = dept.department_id\n" +
            "WHERE dp.is_leave = 0  and (" +
            "<when test='deptIdList !=null'>" +
            "dp.department_id in " +
            " <foreach item='item' index='index' collection='deptIdList' open='(' separator=',' close=')'> " +
            "#{item} \n" +
            " </foreach> " +
            "</when>" +
            "<when test='deptIdList !=null and hospIdList != null'>" +
            "or " +
            "</when>"+
            "<when test='hospIdList != null'>" +
            "dept.hospital_id in" +
            " <foreach item='item' index='index' collection='hospIdList' open='(' separator=',' close=')'>" +
            " #{item} \n" +
            "</foreach> " +
            "</when>" +
            ")" +
            "</script>")
    List<ExtraPatientVo> getAllExtraData(@Param("hospIdList") List<Long> hospIdList, @Param("deptIdList")List<Long> deptIdList);

    @Select("SELECT dp.department_patient_id,dp.department_id,dept.hospital_id FROM bs_department_patient dp \n" +
            "LEFT JOIN bs_department dept on dp.department_id = dept.department_id\n" +
            "WHERE dp.is_leave = 0")
    List<ExtraPatientVo> getAllHospData();

    @Select("<script>SELECT count(1) FROM bs_department_patient dp " +
            "LEFT JOIN bs_patient pat on dp.patient_id = pat.patient_id\n" +
            "LEFT JOIN bs_department dept on dp.department_id = dept.department_id\n" +
            " where dp.is_leave = #{isLeave} " +
            "<when test='departmentId!=null and departmentId!=\"\"'>\n" +
            "   AND dp.department_id = #{departmentId}\n" +
            "    </when>" +
            "<when test='startTime!=null'>\n" +
            "   and dp.hospitalization_date &gt;= #{startTime}\n" +
            "</when>" +
            "<when test='endTime!=null'>\n" +
            "   and dp.hospitalization_date &lt;= #{endTime}\n" +
            "</when>" +
            "<when test='patientName!=null and patientName!=\"\"'>\n" +
            "   and pat.patient_name LIKE CONCAT('%',#{patientName},'%') \n" +
            "</when>" +
            "<when test='hospitalId!=null and hospitalId!=\"\"'>\n" +
            "   and dept.hospital_id = #{hospitalId}\n" +
            "</when>" +
            "<when test='leaveStartTime!=null'>\n" +
            "   and dp.leave_hospital_date &gt;= #{leaveStartTime}\n" +
            "</when>" +
            "<when test='leaveEndTime!=null'>\n" +
            "   and dp.leave_hospital_date &lt;= #{leaveEndTime}\n" +
            "</when>" +
            "</script>" )
    Integer getTotal(Map<String, Object> params);

    @Select("<script>" +
            "SELECT dp.*,pat.patient_name,pat.id_number,pat.gender,dept.department_name,hosp.hospital_name FROM bs_department_patient dp\n" +
            "LEFT JOIN bs_patient pat on dp.patient_id = pat.patient_id\n" +
            "LEFT JOIN bs_department dept on dp.department_id = dept.department_id\n" +
            "LEFT JOIN bs_hospital hosp on dept.hospital_id = hosp.hospital_id\n" +
            "where dp.is_leave = #{isLeave} " +
            "<when test='departmentId!=null and departmentId!=\"\"'>\n" +
            "   AND dp.department_id = #{departmentId}\n" +
            "</when>" +
            "<when test='startTime!=null'>\n" +
            "   and dp.hospitalization_date &gt;= #{startTime}\n" +
            "</when>" +
            "<when test='endTime!=null'>\n" +
            "   and dp.hospitalization_date &lt;= #{endTime}\n" +
            "</when>" +
            "<when test='patientName!=null and patientName!=\"\"'>\n" +
            "   and pat.patient_name LIKE CONCAT('%',#{patientName},'%') \n" +
            "</when>" +
            "<when test='hospitalId!=null and hospitalId!=\"\"'>\n" +
            "   and dept.hospital_id = #{hospitalId}\n" +
            "</when>" +
            "<when test='leaveStartTime!=null'>\n" +
            "   and dp.leave_hospital_date &gt;= #{leaveStartTime}\n" +
            "</when>" +
            "<when test='leaveEndTime!=null'>\n" +
            "   and dp.leave_hospital_date &lt;= #{leaveEndTime}\n" +
            "</when>" +
            " order by dp.department_patient_id desc " +
            "LIMIT #{start},#{limit}" +
            "</script>")
    List<BsDepartmentPatientPageVo> getPageList(Map<String, Object> params);

    @Select("<script>" +
            "SELECT dp.*,pat.patient_name,pat.id_number,pat.gender,dept.department_name,hosp.hospital_name FROM bs_department_patient dp\n" +
            "LEFT JOIN bs_patient pat on dp.patient_id = pat.patient_id\n" +
            "LEFT JOIN bs_department dept on dp.department_id = dept.department_id\n" +
            "LEFT JOIN bs_hospital hosp on dept.hospital_id = hosp.hospital_id\n" +
            "where dp.is_leave = #{isLeave} " +
            "<when test='departmentId!=null and departmentId!=\"\"'>\n" +
            "   AND dp.department_id = #{departmentId}\n" +
            "</when>" +
            "<when test='startTime!=null'>\n" +
            "   and dp.hospitalization_date &gt;= #{startTime}\n" +
            "</when>" +
            "<when test='endTime!=null'>\n" +
            "   and dp.hospitalization_date &lt;= #{endTime}\n" +
            "</when>" +
            "<when test='patientName!=null and patientName!=\"\"'>\n" +
            "   and pat.patient_name LIKE CONCAT('%',#{patientName},'%') \n" +
            "</when>" +
            "<when test='hospitalId!=null and hospitalId!=\"\"'>\n" +
            "   and dept.hospital_id = #{hospitalId}\n" +
            "</when>" +
            "<when test='leaveStartTime!=null'>\n" +
            "   and dp.leave_hospital_date &gt;= #{leaveStartTime}\n" +
            "</when>" +
            "<when test='leaveEndTime!=null'>\n" +
            "   and dp.leave_hospital_date &lt;= #{leaveEndTime}\n" +
            "</when>" +
//            "<when test='list!=null'>\n" +
//            "  and dp.department_patient_id in " +
//            "   <foreach item='item' index='index' collection='list' open='(' separator=',' close=')'> #{item} </foreach> \n" +
//            "    </when>" +
            "<when test='departmentPatientId!=null and departmentPatientId!=\"\"'>\n" +
            "   and dp.department_patient_id = #{departmentPatientId} \n" +
            "</when>" +
            " order by dp.department_patient_id desc" +
            "</script>")
    List<BsDepartmentPatientPageVo> exportList(Map<String, Object> params);

    @Select("<script>" +
            "SELECT * FROM bs_department_patient WHERE is_leave = 1 and DATE_SUB(CURDATE(), INTERVAL #{day} DAY) >= date(leave_hospital_date)" +
            "</script>")
    List<BsDepartmentPatientEntity> getListByDay(@Param("day") int day);
}
